package xiaopiya.robot;

import java.util.List;

import org.jivesoftware.smack.XMPPException;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 主程序
 * 
 * @author Ray (ayufox@gmail.com)
 * @version 0.1
 */
public class Main extends Thread
{
	private static List<XiaoPiYaRobot> robots;

	private static ClassPathXmlApplicationContext ctx = null;

	public static void main(String[] args) throws XMPPException
	{
		ctx = new ClassPathXmlApplicationContext(
				"classpath:/config/spring*.xml");
		robots = (List<XiaoPiYaRobot>) ctx.getBean("robots");

		Runtime.getRuntime().addShutdownHook(new Main());
		for (XiaoPiYaRobot robot : robots)
		{
			robot.startup();
		}
		while (true)
		{
			try
			{
				Thread.sleep(5000);
			} catch (InterruptedException e)
			{
				e.printStackTrace();
			}
		}
	}

	public void run()
	{
		if (robots != null)
		{
			for (XiaoPiYaRobot robot : robots)
			{
				try
				{
					robot.shutdown();
				} catch (Throwable t)
				{
					t.printStackTrace();
				}
			}
		}

		if (ctx != null)
		{
			ctx.destroy();
		}
	}
}
